#include <stdio.h>

#include "nuclei_sdk_hal.h"

#define QSPI_SCKMODE_CPOL 1
#define QSPI_SCKMODE_CPHA 0

int i=0;

uint8_t spi_transmit_receive(uint8_t tx)
{
    QSPI_TXDATA(QSPI0)=tx;
    while(QSPI_STATUS(QSPI0) & (1<<0));
    return QSPI_RXDATA(QSPI0) & 0xff;
}

int main(){
    printf("test start\r\n");

    /*config SCK CPOL and CPHA*/
    qspi_sckmode_cfg(QSPI0,~(QSPI_SCKMODE_PHA|QSPI_SCKMODE_POL));
    /*disable flashxip mode*/
    qspi_fctrl_flashxip_disable(QSPI0);
    /*config CSMODE as OFF*/
    qspi_csmode_cfg(QSPI0,SPI_OFF);
    /*config SCK frequency divider*/
    qspi_sckdiv_cfg(QSPI0,0x01);


    for( i = 1;i<4;i++)
    {
        QSPI0_REG(SPI_REG_CSDEF) &=~(0x1);
        printf("QSPI0_REG(SPI_REG_CSDEF)=0x%08x\r\n",QSPI0_REG(SPI_REG_CSDEF));
        spi_transmit_receive(i);
        printf("QSPI_TXDATA(QSPI0)=0x%08x\r\n",QSPI_TXDATA(QSPI0));
        QSPI0_REG(SPI_REG_CSDEF)|=0x01;
        printf("QSPI0_REG(SPI_REG_CSDEF)=0x%08x\r\n",QSPI0_REG(SPI_REG_CSDEF));
    }
    #ifdef CFG_SIMULATION
        pass_fail_simulation(1);
    #endif 
    printf("test finish\r\n");
    while(1);

}
